// ignore: unused_import
import 'dart:ffi';
import 'dart:io';
import 'dart:isolate';
import 'dart:math';
// ignore: unused_import
import 'dart:typed_data';

import 'package:battery/battery.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

main(List<String> args) {
  runApp(Basewhidget());
}

class Basewhidget extends StatefulWidget {
  @override
  _BasewhidgetState createState() => _BasewhidgetState();
}

class _BasewhidgetState extends State<Basewhidget>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;

  @override
  void initState() {
    _controller = AnimationController(vsync: this);
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  var nums = 11000.00;
  var battery = Battery();
  var states = 0;
  var zt = 0;
  @override
  Widget build(BuildContext context) {
    // Instantiate it

    // Acess current battery level
    print("充电状态： ");
    this.battery.onBatteryStateChanged.listen((BatteryState state) async {
      // Do something with new state

      if (state.index == 1) {
        print("充电状态");
        this.states = 1;
        // SaveDates();
        // if (this.zt == 0) {
        //   this.zt = 1;
        // } else {
        //   this.zt = 0;
        // }
      } else if (state.index == 2) {
        this.states = 2;
        print("放电状态");
      } else {
        this.states = 0;
        // print("冲满电了");
      }

      if (this.states == 1) {
        if (this.zt == 0) {
          ReceivePort receivePort = ReceivePort();
          // SendPort new_send;

          this.zt = 1;
          // var iso = await Isolate.spawn(GetMoneyNum, receivePort.sendPort);

          var iso = await Isolate.spawn(GetMoneyNum, receivePort.sendPort);
          // receivePort.sendPort.send(this.nums);
          // sleep(const Duration(seconds: 2));

          receivePort.listen((message) {
            // print("这是收到请求数据： ${message}");

            setState(() {
              this.nums += message;
              String bbb = this.nums.toString(); // 将数据转成字符串
              int index = bbb.indexOf("."); // 查找.的位置

              if (bbb.substring(index, bbb.length).length > 2) {
                bbb = bbb.substring(0, index + 3);
              } else {
                bbb = bbb.substring(0, index + 2) + "0";
              }
              this.nums = double.parse(bbb);
              // print("bbb = ${bbb}");
            });
            // print("当前的nums是： ${this.nums}, this.nums 是 ${this.nums}");

            // new_send = message[1] as SendPort;
            // new_send.send(this.nums);
            if (this.states != 1) {
              receivePort.close();
              iso.kill(priority: Isolate.immediate);
              this.zt = 0;
            }
          });
        }
      }
    });

    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
          // backgroundColor: Color.fromRGBO(128, 138, 135, 1),
          appBar: AppBar(
            elevation: 0,
            title: Text("余额"),
            leading: Icon(Icons.arrow_back_ios), // 左导航栏图标
            actions: <Widget>[
              IconButton(icon: Icon(Icons.more_horiz), onPressed: () {})
            ],
          ),
          body: Container(
            // color: Color.fromRGBO(128, 138, 135, 1),
            child: Column(
              children: <Widget>[
                Container(
                    height: 40,
                    color: Colors.blue,
                    child: ListTile(
                      title: Text(
                        "余额(元)",
                        style:
                            TextStyle(color: Color.fromRGBO(255, 255, 255, 1)),
                      ),
                    )),
                Container(
                    height: 80,
                    color: Colors.blue,
                    child: ListTile(
                      title: Text(
                        "${this.nums}",
                        style: TextStyle(fontSize: 40, color: Colors.white),
                      ),
                    )),
                Container(
                  color: Colors.white,
                  child: ListTile(
                    leading: Icon(
                      Icons.branding_watermark,
                      color: Colors.blue,

                      // size: 20,可以设置大小
                    ),
                    onTap: () {
                      this.nums += 0.01;
                      String nnn = this.nums.toString();
                      int index = nnn.indexOf(".");
                      if (nnn.substring(index, nnn.length).length > 2) {
                        nnn = nnn.substring(0, index + 3);
                      } else {
                        nnn = nnn.substring(0, index + 2) + "0";
                      }

                      setState(() {
                        this.nums = double.parse(nnn);
                      });
                    },
                    title: Text("充值"),
                    trailing: Icon(
                      Icons.keyboard_arrow_right,
                    ),
                  ),
                ),
                Divider(
                  height: 1.0,
                  indent: 60.0,
                  color: Color.fromRGBO(128, 138, 135, 1),
                ),
                Container(
                  color: Colors.white,
                  child: ListTile(
                    leading: Icon(
                      Icons.account_balance_wallet,
                      color: Colors.orange,
                    ),
                    title: Text("提现"),
                    trailing: Icon(Icons.keyboard_arrow_right),
                    onTap: () {},
                  ),
                ),
                Container(height: 15, color: Color.fromRGBO(235, 235, 235, 1)),
                Container(
                  color: Colors.white,
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        flex: 2,
                        child: ListTile(
                          leading: Icon(
                            Icons.store,
                            color: Colors.blue,
                          ),
                          title: Text("转账"),
                        ),
                      ),
                      Expanded(
                          flex: 2,
                          child: ListTile(
                            title: Text(
                              "支持批量转钱",
                              textAlign: TextAlign.right,
                              style: TextStyle(
                                color: Color.fromRGBO(128, 138, 135, 1),
                              ),
                            ),
                            trailing: Icon(Icons.keyboard_arrow_right),
                          ))
                    ],
                  ),
                ),
                Divider(
                  height: 1.0,
                  indent: 60.0,
                  color: Color.fromRGBO(128, 138, 135, 1),
                ),
                Container(
                  color: Colors.white,
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        flex: 2,
                        child: ListTile(
                          leading: Icon(
                            Icons.supervised_user_circle,
                            color: Color.fromRGBO(255, 80, 0, 1),
                          ),
                          title: Text("集分宝"),
                        ),
                      ),
                      Expanded(
                          flex: 2,
                          child: ListTile(
                            title: Text(
                              "做任务送集分宝",
                              textAlign: TextAlign.right,
                              style: TextStyle(
                                color: Color.fromRGBO(128, 138, 135, 1),
                              ),
                            ),
                            trailing: Icon(Icons.keyboard_arrow_right),
                          ))
                    ],
                  ),
                ),
                Divider(
                  height: 1.0,
                  indent: 60.0,
                  color: Color.fromRGBO(128, 138, 135, 1),
                ),
                Container(
                  color: Colors.white,
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        flex: 2,
                        child: ListTile(
                          leading: Icon(
                            Icons.monetization_on,
                            color: Colors.blue,
                          ),
                          title: Text("备用金"),
                        ),
                      ),
                      Expanded(
                          flex: 2,
                          child: ListTile(
                            title: Text(
                              "最高500元用7天",
                              textAlign: TextAlign.right,
                              style: TextStyle(
                                color: Color.fromRGBO(128, 138, 135, 1),
                              ),
                            ),
                            trailing: Icon(Icons.keyboard_arrow_right),
                          ))
                    ],
                  ),
                ),
                Divider(
                  height: 1.0,
                  color: Color.fromRGBO(235, 235, 235, 1),
                ),
                Spacer(),
                Container(
                  height: 60,
                  child: Center(
                      child: Row(
                    children: <Widget>[
                      Expanded(
                          child: Text(
                        "信用借款",
                        style: TextStyle(color: Colors.blue),
                        textAlign: TextAlign.right,
                      )),
                      Text(
                        " | ",
                        style:
                            TextStyle(color: Color.fromRGBO(150, 150, 150, 1)),
                      ),
                      Expanded(
                          child: Text(
                        "常见问题",
                        style: TextStyle(color: Colors.blue),
                      )),
                    ],
                  )),
                )
              ],
            ),
          )),
    );
  }
}

void GetMoneyNum(SendPort sendPort) {
  var rng = new Random();
  double nums = 0;
  // ReceivePort new_rp = ReceivePort();
  // sendPort.send("这是Getmoneynum 函数请求数据：");

  // SendPort new_send = new_rp.sendPort;

  // sendPort.send([0, new_send]); // 发送主线程和GetMoneyNum之间的通信 sendport

  // new_rp.listen((message) {
  //   // 接收主线程发送过来的数据
  //   print("这是GetMoneynum接收到的数据： ${message}");
  //   nums = message;
  // });
  while (1 == 1) {
    var randnum = rng.nextInt(100);

    var nnn = "0." + randnum.toString();
    nums += double.parse(nnn); // 添加数据

    sendPort.send(nums);
    nums = 0;
    //下面这句用便是
    sleep(const Duration(microseconds: 1000));
  }
  ;
}
